package com.youha.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @author youha
 * @create 2021-08-16 22:39
 */
public class ContextLoaderListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();

        //找的是web.xml中的全局初始化参数，context
        String parameter = servletContext.getInitParameter("contextConfigLocation");

        ApplicationContext context = new ClassPathXmlApplicationContext(parameter);


        servletContext.setAttribute("context",context);

        System.out.println("spring容器创建成功");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
